﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// 环境扩展类
    /// </summary>
    public partial class EnvironmentEx
    {
        static EnvironmentEx()
        {
            var mfileName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName).ToLower();
            if (mfileName.Contains("w3wp.exe") || mfileName.Contains("iisexpress.exe"))
            {
                _appType = AppType.WebApp;
            }
        }

        private static AppType _appType = AppType.Exe;
        /// <summary>
        /// 获取当前环境是否是WebApp环境[true:WebApp;false:exe]
        /// </summary>
        public static AppType AppType
        {
            get
            {
                return _appType;
            }
        }

        ///// <summary>
        ///// 使用指定的应用程序配置文件作为 System.Configuration.Configuration 对象打开以允许读或写操作。
        ///// </summary>
        ///// <returns>System.Configuration.Configuration</returns>
        //public static System.Configuration.Configuration OpenWebConfiguration()
        //{
        //    System.Configuration.Configuration config;
        //    if (_appType == AppType.WebApp)
        //    {
        //        config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
        //    }
        //    else
        //    {
        //        config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
        //    }

        //    return config;
        //}


        /// <summary>
        /// 获取当前操作系统类型
        /// </summary>
        public static OSPlatformID OS
        {
            get
            {
                OSPlatformID os;
                switch (Environment.OSVersion.Platform)
                {
                    case PlatformID.Win32NT:
                    case PlatformID.Win32S:
                    case PlatformID.Win32Windows:
                        os = OSPlatformID.Win;
                        break;
                    case PlatformID.Unix:
                        os = OSPlatformID.Linux_Unix;
                        break;
                    case PlatformID.MacOSX:
                        os = OSPlatformID.MacOS;
                        break;
                    default:
                        os = OSPlatformID.Other;
                        break;
                }

                return os;
            }
        }

        /// <summary>
        /// 将PlatformID转换为OSPlatformID
        /// </summary>
        /// <param name="platformID">PlatformID</param>
        /// <returns>OSPlatformID</returns>
        public static OSPlatformID CovnerTo(PlatformID platformID)
        {
            OSPlatformID os;
            switch (Environment.OSVersion.Platform)
            {
                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                    os = OSPlatformID.Win;
                    break;
                case PlatformID.Unix:
                    os = OSPlatformID.Linux_Unix;
                    break;
                case PlatformID.MacOSX:
                    os = OSPlatformID.MacOS;
                    break;
                default:
                    os = OSPlatformID.Other;
                    break;
            }

            return os;
        }


        //CPU小端:高位数据存放在高地址,低位数据存放在低地址
        //CPU大端:高位数据存放在低地址,低位数据存放在高地址

        /// <summary>
        /// 当前CPU类型
        /// </summary>
        private static CPUTypes? _cpuType = null;
        /// <summary>
        /// 获取当前当前CPU类型
        /// </summary>
        public static CPUTypes CpuType
        {
            get
            {
                if (_cpuType == null)
                {
                    int i = 1;
                    byte[] buffer = BitConverter.GetBytes(i);
                    if (buffer[0] == 1)
                    {
                        //小端
                        _cpuType = CPUTypes.LittleEndian;
                    }
                    else
                    {
                        //大端
                        _cpuType = CPUTypes.LargeEndian;
                    }
                }
                return _cpuType.Value;
            }
        }

    }

}
